package io.renren.modules.mail.service.impl;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import io.renren.modules.mail.FileUtil.POIUtil;
import io.renren.modules.mail.dao.PaperDao;
import io.renren.modules.mail.email.MailInfo;
import io.renren.modules.mail.pojo.Paper;
import io.renren.modules.mail.service.InsertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

@Service
public class InsertServiceImpl implements InsertService {

    @Autowired(required = false)
    PaperDao paperDao;
    @Override
    public void insert() {
        //插入到本地
        MailInfo re = null;
        Paper paper = new Paper();
        POIUtil poiUtil = new POIUtil();
        paperDao.deletePapers(paper);
        try
        {
            String host = "pop.qq.com";
            String username = "841852080@qq.com";
            String password = "tuivddedequpbdeh";
            Properties p = new Properties();
            p.setProperty("mail.pop3.host", "pop.qq.com"); // 按需要更改
            p.setProperty("mail.pop3.port", "995");
            // SSL安全连接参数
            p.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            p.setProperty("mail.pop3.socketFactory.fallback", "true");
            Session session;
            session = Session.getDefaultInstance(p, null);
            Store store = session.getStore("pop3");
            store.connect(host, username, password);
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message message[] = folder.getMessages();
            System.out.println("邮件数量:　" + message.length);
            for (int i = 0; i < message.length; i++) {
                try {
                    re = new MailInfo((MimeMessage) message[i]);

                    /*-----------------导入数据库和本地---------------*/
                    if(re.isContainAttach((Part) message[i])){
                        re.saveAttachMent((MimeMessage) message[i]);//保存文件到本地
                        String path = re.getFilePath();
                        //导入到数据库
                        String doc1 = poiUtil.ceshi(path);
                        String name = doc1.substring(doc1.indexOf("姓名")+3,doc1.indexOf("姓名")+7);
                        paper.setName(name);
                        String age = doc1.substring(doc1.indexOf("年龄")+3,doc1.indexOf("年龄")+6);
                        paper.setAge(age);
                        String job = doc1.substring(doc1.indexOf("求职意向")+5,doc1.indexOf("求职意向")+11);
                        paper.setJob(job);
                        String workedyears = doc1.substring(doc1.indexOf("工作年限")+5,doc1.indexOf("工作年限")+7);
                        Integer workedyear = Integer.valueOf(workedyears);
                        paper.setWorkedyear(workedyear);
                        String moneys = doc1.substring(doc1.indexOf("薪酬范围")+5,doc1.indexOf("薪酬范围")+10);
                        Integer money = Integer.valueOf(moneys);
                        paper.setMoney(money);
                        String phone = doc1.substring(doc1.indexOf("电话")+3,doc1.indexOf("电话")+14);
                        paper.setPhone(phone);
                        String dateStr = re.getSentDate();
                        SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
                        Date date = sf.parse(dateStr);
                        paper.setDate(date);
                        paperDao.insertPaper(paper);
                    }
                    /*-----------------导入数据库----------------*/
                } catch (MessagingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (javax.mail.NoSuchProviderException e) {
            e.printStackTrace();
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }
    }
}
